<?php

namespace App\Constants;

/**
 * @Constants
 */
class ResponseArray
{
    public static function renderError($message, $data = null)
    {
        return [
            'status_code' => 500,
            'message'     => $message ? $message : '服务器错误,请重试.',
            'result'      => $data,

        ];
    }

    /**
     * 服务器理解请求客户端的请求，但是拒绝执行此请求
     *
     * @param string $message
     * @return array
     */
    public static function forbiddenError($message = '')
    {
        return [
            'status_code' => 412,
            'result'      => null,
            'message'     => '请求被拒绝 ' . $message,
        ];
    }

    /**
     * 服务器理解请求客户端的请求，但是业务执行此请求
     *
     * @param string $message
     * @return array
     */
    public static function abortMessage($message = '')
    {
        return [
            'status_code' => 422,
            'result'      => false,
            'message'     => $message,
        ];
    }


    public static function parameterError($message = '', $rules = null)
    {
        $implode = function ($rules) {
            //return $rules;

            if (!empty($rules)) {
                foreach ($rules as &$rule) {
                    if (is_array($rule)) {
                        $rule = array_values($rule);
                        $rule = json_encode($rule);
                        $rule = str_replace('","', "|", $rule);
                        $rule = str_replace('"', '', $rule);
                        $rule = str_replace('[', '', $rule);
                        $rule = str_replace(']', '', $rule);
//                        $rule = str_replace('{', '', $rule);
//                        $rule = str_replace('}', '', $rule);
                        //$rule = implode('|', $rule);
                        //$rule = rtrim($rule, '|');
                    }
                }
                return $rules;
            }

        };
        return [
            'status_code' => 412,
            'result'      => null,
            'message'     => $message ? $message : 'Request parameter error.',
            'rules'       => $implode($rules)
        ];
    }

    public static function notFoundError($message = '')
    {
        return [
            'status_code' => 404,
            'result'      => null,
            'message'     => '资源不存在 ' . $message,
        ];
    }

    public static function unauthenticatedError($message = null )
    {
        return [
            'status_code' => 401,
            'result'      => null,
            'message'     => $message ?? '密码验证未通过,请重新登录.',
        ];
    }

    public static function tokenExpiredError($message = null )
    {
        return [
            'status_code' => 401,
            'result'      => null,
            'message'     => $message ?? '登录令牌已过期,请重新登录.',
        ];
    }

    public static function response($data, $message = '')
    {
        return [
            'status_code' => 200,
            'result'      => $data,
            'message'     => $message ? $message : '请求成功.',
        ];
    }

    public static function excelResponse($data, $message = '')
    {
        return [
            'status_code' => 200,
            'result'      => $data,
            'message'     => $message,
        ];
    }

    public static function excelParameterError($message = '', $rules = null)
    {
        $implode = function ($rules) {
            //return $rules;

            if (!empty($rules)) {
                foreach ($rules as &$rule) {
                    if (is_array($rule)) {
                        $rule = array_values($rule);
                        $rule = json_encode($rule);
                        $rule = str_replace('","', "|", $rule);
                        $rule = str_replace('"', '', $rule);
                        $rule = str_replace('[', '', $rule);
                        $rule = str_replace(']', '', $rule);
//                        $rule = str_replace('{', '', $rule);
//                        $rule = str_replace('}', '', $rule);
                        //$rule = implode('|', $rule);
                        //$rule = rtrim($rule, '|');
                    }
                }
                return $rules;
            }

        };
        return [
            'status_code' => 412,
            'result'      => null,
            'message'     => $message,
            'rules'       => $implode($rules)
        ];
    }

    public static function AttemptsError($message, $data = null)
    {
        return [
            'status_code' => 405,
            'message'     => $message ? $message : '请求过于频繁.',
            'result'      => $data,

        ];
    }
}
